<template>
  <div class="upload-page">
    <!-- 上传区域 -->
    <div class="upload-container">
      <!-- 图片上传区域 -->
      <div class="image-upload-section">
        <div 
          class="image-upload-box" 
          @click="triggerFileInput"
          v-if="!imagePreview"
        >
          <i class="upload-icon">+</i>
          <span>点击上传图片</span>
        </div>
        <div class="preview-container" v-else>
          <img :src="imagePreview" class="preview-image" />
          <button class="remove-image" @click="removeImage">×</button>
        </div>
        <input
          type="file"
          ref="fileInput"
          @change="handleFileChange"
          accept="image/*"
          style="display: none"
        />
      </div>

      <!-- 描述输入区域 -->
      <div class="description-section">
        <div class="input-group">
          <input
            v-model="simpleDescription"
            type="text"
            class="input-field"
            placeholder="简单描述"
          />
        </div>
        <div class="input-group">
          <textarea
            v-model="detailedDescription"
            class="input-field textarea"
            placeholder="详细描述"
            rows="4"
          ></textarea>
        </div>
      </div>

      <!-- 按钮区域 -->
      <div class="button-group">
        <button class="submit-btn" @click="handleSubmit">提交</button>
        <button class="cancel-btn" @click="handleCancel">放弃</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import axios from 'axios';

const imagePreview = ref('');
const simpleDescription = ref('');
const detailedDescription = ref('');
const fileInput = ref(null);
const router = useRouter();

const triggerFileInput = () => {
  fileInput.value.click();
};

const handleFileChange = (event) => {
  const file = event.target.files[0];
  if (file) {
    const reader = new FileReader();
    reader.onload = (e) => {
      imagePreview.value = e.target.result;
    };
    reader.readAsDataURL(file);
  }
};

const removeImage = () => {
  imagePreview.value = '';
  if (fileInput.value) {
    fileInput.value.value = '';
  }
};

const handleSubmit = async () => {
  if (!imagePreview.value) {
    alert('请上传图片');
    return;
  }
  if (!simpleDescription.value) {
    alert('请输入简单描述');
    return;
  }
  if (!detailedDescription.value) {
    alert('请输入详细描述');
    return;
  }

  try {
    // 创建 WebSocket 连接
    const ws = new WebSocket('ws://127.0.0.1:8080/data/upload');

    // 监听连接打开
    ws.onopen = () => {
      // 准备要发送的数据
      const uploadData = {
        image: imagePreview.value,
        simpleDescription: simpleDescription.value,
        detailedDescription: detailedDescription.value
      };

      // 发送数据
      ws.send(JSON.stringify(uploadData));
    };

    // 监听消息接收
    ws.onmessage = (event) => {
      const response = JSON.parse(event.data);
      const uploadId = response.id; // 假设返回数据中包含 id

      // 关闭 WebSocket 连接
      ws.close();

      // 跳转到详情页面
      router.push(`/upload/detail/${uploadId}`);
    };

    // 监听错误
    ws.onerror = (error) => {
      console.error('WebSocket 错误:', error);
      alert('上传失败，请重试');
    };

  } catch (error) {
    console.error('上传失败:', error);
    alert('上传失败，请重试');
  }
};

const handleCancel = () => {
  router.back();
};
</script>

<style scoped>
.upload-page {
  padding: 20px;
  max-width: 800px;
  margin: 0 auto;
  margin-top: 80px;
}

.upload-container {
  background: #ffffff;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.image-upload-section {
  margin-bottom: 30px;
}

.image-upload-box {
  width: 100%;
  height: 200px;
  border: 2px dashed #dcdfe6;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  transition: all 0.3s ease;
}

.image-upload-box:hover {
  border-color: #409eff;
  background-color: #f5f7fa;
}

.upload-icon {
  font-size: 40px;
  color: #909399;
  margin-bottom: 10px;
}

.preview-container {
  position: relative;
  width: 100%;
}

.preview-image {
  width: 100%;
  max-height: 300px;
  object-fit: contain;
  border-radius: 8px;
}

.remove-image {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.5);
  color: white;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
}

.input-group {
  margin-bottom: 20px;
}

.input-field {
  width: 100%;
  padding: 12px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  font-size: 14px;
  transition: border-color 0.3s;
}

.input-field:focus {
  border-color: #409eff;
  outline: none;
}

.textarea {
  min-height: 120px;
  resize: vertical;
}

.button-group {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-top: 30px;
}

.submit-btn, .cancel-btn {
  padding: 12px 40px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
  font-weight: 500;
  transition: all 0.3s ease;
}

.submit-btn {
  background-color: #409eff;
  color: white;
}

.cancel-btn {
  background-color: #f56c6c;
  color: white;
}

.submit-btn:hover {
  background-color: #66b1ff;
}

.cancel-btn:hover {
  background-color: #f78989;
}
</style>
